package com.team7.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team7.common.entity.User;
import com.team7.common.entity.UserRole;
import com.team7.common.utils.WebUtil;
import com.team7.common.vo.CommonResult;
import com.team7.common.vo.UserVo;
import com.team7.system.dao.UserDao;
import com.team7.system.dao.UserRoleDao;
import com.team7.system.feign.UserFeign;
import com.team7.system.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @program: team7-Object
 * @description:
 * @author: 杨海涛
 * @create: 2021-10-21 14:46
 **/
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    @Resource
    private UserRoleDao userRoleDao;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private UserFeign userFeign;

    @Override
    public List<String> findUserPermissionById(String userId) {
        return userDao.findUserPermissionById(userId);
        //return null;
    }

    @Override
    public User findUserByUserName(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("username",username);
        return userDao.selectOne(wrapper);
        //return null;
    }

    @Override
    public CommonResult findUserByPage(Integer page, Integer limit, UserVo userVo) {
        Page<User> page1=new Page<>(page,limit);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //1.判断用户名是否为空
        if (StringUtils.isNotEmpty(userVo.getUsername())){
            wrapper.like("username",userVo.getUsername());
        }

        //2.判断开始时间是否为空
        if (userVo.getStartDate()!=null){
            wrapper.ge("gmt_create",userVo.getStartDate());
        }

        //3.判断结束时间是否为空
        if (userVo.getEndDate()!=null){
            wrapper.le("gmt_create",userVo.getEndDate());
        }
        Page<User> userPage = userDao.selectPage(page1, wrapper);
        return new CommonResult(2000,"查找成功",userPage);
        //return null;
    }

    @Override
    public CommonResult updateUserStatusByid(String id, Integer deleted) {

        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        List<UserRole> userRoles = userRoleDao.selectList(wrapper);
        for (UserRole userRole:userRoles) {
            //除去超级管理员
            if ("1".equals(userRole.getRoleId())){
                return new CommonResult(5000,"放肆，超级管理员无法禁用",null);
            }
        }
        User user = new User();
        user.setId(id);
        user.setIsDeleted(deleted);
        user.setGmtModified(LocalDateTime.now().toString());
        userDao.updateById(user);
        return new CommonResult(2000,"状态修改成功",null);
        //return null;
    }

    @Override
    public CommonResult addUserByUser(User user) {
        user.setPassword("2a7e188fb7243643d7ecea6667b2b7ff");
        String salt= "aaaaaa";
        user.setSalt(salt);
        user.setGmtCreate(LocalDateTime.now().toString());
        user.setGmtModified(LocalDateTime.now().toString());

        int insert = userDao.insert(user);
        return new CommonResult(2000,"插入成功",null);
        //return null;
    }

    @Override
    public CommonResult updateUserByUser(User user) {
        userDao.updateById(user);
        return new CommonResult(2000,"用户信息修改成功",null);
        //return null;
    }

    @Override
    @Transactional//声明式事务管理
    public CommonResult deleteUserById(String id) {

        int i = userDao.deleteById(id);
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        int delete = userRoleDao.delete(wrapper);

        return new CommonResult(2000,"删除成功",null);
        //return null;
    }

    @Override
    public CommonResult updateUserPassword(String oldPassword, String newPassword) {
        //获取请求头的token值
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");

        //将String转为对象类型
        String s = redisTemplate.opsForValue().get(token);
        User user = JSON.parseObject(s, User.class);

        Map<String, String> map = userFeign.updateUserPassword(oldPassword, newPassword, user.getSalt());

        String oldPassword2 = map.get("oldPassword");
        String newPassword2 = map.get("newPassword");
        String newSalt2 = map.get("newSalt");

        System.out.println(oldPassword2);
        System.out.println(newPassword2);
        System.out.println(newSalt2);

        if (user.getPassword().equals(oldPassword2)){//密码相同
            user.setPassword(newPassword2);
            user.setSalt(newSalt2);
            userDao.updateById(user);

            return new CommonResult(2000,"密码修改成功",null);
        }else {
            return new CommonResult(5000,"原密码错误",null);

        }
        //return null;
    }
}
